/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.redot.repository;

import com.yunqian.redot.domain.CoursesOrder;
import com.yunqian.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 客户课程报名记录
 *
 * @author zsy
 * 2018-09-29 15:54:11
 */
@Repository
public interface CoursesOrderRepository extends BaseRepository<CoursesOrder, Long> {

    /**
     * 将30分钟内未付款的订单状态改为3（取消）
     */
    @Modifying
    @Query(value = "update t_courses_order set status = 3 where status = 1 and now() > date_add(save_time, interval 30 minute)", nativeQuery = true)
    void updateOrderStatus();

    /**
     * 根据订单号查询
     * @param orderNo
     * @return
     */
    CoursesOrder findByOrderNo(String orderNo);

    /**
     * 多条件查询
     * @param orderNo
     * @param phone
     * @param beginTime
     * @param endTime
     * @return
     */
    @Query(value = "select * from t_courses_order co where if(:orderNo != '', order_no = :orderNo, 1 = 1) and if(:phone != '', phone = :phone, 1 = 1) and save_time between :beginTime and :endTime", nativeQuery = true)
    List<CoursesOrder> findByOrderNoAndPhoneAndRangeTime(@Param("orderNo") String orderNo, @Param("phone") String phone, @Param("beginTime") String beginTime, @Param("endTime") String endTime);

    /**
     * 查找用户是否报名过该课程
     * @param customerId
     * @param courseId
     * @return
     */
    boolean existsByCustomerIdAndCourseId(Long customerId, Long courseId);

}
